// lib/routes/app_router.dart
import 'package:flutter/material.dart';
import 'package:stage6_examination/pages/login/index.dart';
import 'package:stage6_examination/utils/token.dart';
import 'package:stage6_examination/pages/tab_bar.dart';

class AppRouter {
  // 路由名称常量（避免硬编码）
  static const String home = '/';
  static const String login = '/login';

  // 静态路由表（无需参数的路由）
  static Map<String, WidgetBuilder> get routes => {
    home: (context) => const TabBarPage(),
  };

  // 动态路由生成（带参数或逻辑的路由）
  static Route<dynamic>? onGenerateRoute(RouteSettings settings) {
    final tokenManager = TokenManager();
    final token = tokenManager.getToken() ?? '';

    // Token 验证逻辑
    if (token.isEmpty && settings.name != login) {
      return MaterialPageRoute(builder: (_) => const LoginPage());
    }

    // 根据路由名称返回对应页面
    switch (settings.name) {
      default:
        return MaterialPageRoute(builder: (_) => const TabBarPage());
    }
  }
}
